const orderlist = require('../models/orderlist')

// 获取全部订单
const getOrders = async (ctx) => {
  const data = await orderlist.getOrders()
  if(data) {
    ctx.body = {
      code: 0,
      message: '成功',
      data
    }
  }
}
// 获取全部订单
const getAllOrder = async (ctx) => {
  const query = ctx.request.query
  const data = await orderlist.getAllOrder(query)
  if(data) {
    ctx.body = {
      code: 0,
      message: '成功',
      data
    }
  }
}
// 根据id获取订单
const getOrderById = async (ctx) => {
  const id = ctx.params.id
  const data = await orderlist.getOrderById(id)
  if(data) {
    ctx.body = {
      code: 0,
      message: '成功',
      data
    }
  }
}
// 创建订单
const addOrder = async (ctx) => {
  const data = ctx.request.body
  const result = await orderlist.addOrder(data)
  if(result) {
    ctx.body = {
      code: 0,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 修改订单状态
const updateOrderStatus = async (ctx) => {
  const data = ctx.request.body
  const id = ctx.params.id
  const result = await orderlist.updateOrderStatus(id, data)
  if(result) {
    ctx.body = {
      code: 0,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 根据关键字获取订单
const getOrderByKeyWords = async (ctx) => {
  const keywords = ctx.request.query
  const data = await orderlist.getOrderByKeyWords(keywords)
  if(data) {
    ctx.body = {
      code: 0,
      message: '成功',
      data
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
module.exports = {
  getOrders,
  getAllOrder,
  getOrderById,
  addOrder,
  updateOrderStatus,
  getOrderByKeyWords
}